public class Leet695 {
    static int num;
    public int maxAreaOfIsland(int[][] grid) {
        num = 0;
        int max = 0;
        for (int i = 0; i < grid.length; i++) {
            for (int j = 0; j < grid[0].length; j++) {
                if (grid[i][j]==1){
                    dfs(grid,i,j);
                    max = Math.max(num,max);
                    num = 0;
                }
            }
        }
        return max;
    }
    static void dfs(int[][] grid,int i,int j){
        grid[i][j] = 3;
        num++;
        if (i>0&&grid[i-1][j]==1)
            dfs(grid,i-1,j);
        if (j>0&&grid[i][j-1]==1)
            dfs(grid,i,j-1);
        if (i<grid.length-1&&grid[i+1][j]==1)
            dfs(grid,i+1,j);
        if (j<grid[0].length&&grid[i][j+1]==1)
            dfs(grid,i,j+1);
    }
}
